package com.renyi.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.alibaba.fastjson.JSON;
import com.google.gson.reflect.TypeToken;
import com.renyi.http.HttpResult;
import com.renyi.http.ParamBean;
import com.renyi.model.MedicineAppInfo;
import com.renyi.model.MybatisService;
import com.renyi.util.JSONUtils;

/**
 * 
 * @author shs
 * 
 */
@Controller
public class MedicineController extends BaseController {
	@Autowired
	private MybatisService mybatisService;

	// 患者-获取药品模块列表
	@RequestMapping("getMedicineModule")
	public void getMedicineModule(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用戶ID参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getMedicineModuleSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取药品分类列表
	@RequestMapping("getMedicineType")
	public void getMedicineType(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String moduleID = paramMap.get("moduleID") != null ? paramMap.get("moduleID").toString() : "";

			if ("".equals(moduleID)) {
				result.setCode(-1); // 0：成功
				result.setMsg("药品模块编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("uid", uid);
			pMap.put("moduleID", moduleID);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getMedicineTypeSQL", pMap);
			result.setCode(0); // 0：成功
			result.setData(data);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取药品列表
	@RequestMapping("getMedicineList")
	public void getMedicineList(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String typeID = paramMap.get("typeID") != null ? paramMap.get("typeID").toString() : "";

			Integer page = (int) (paramMap.get("page") != null
					? Math.floor(Float.valueOf(paramMap.get("page").toString())) : 1);
			Integer pageSize = (int) (paramMap.get("pageSize") != null
					? Math.floor(Float.valueOf(paramMap.get("pageSize").toString())) : 10);

			if ("".equals(uid)) {
				result.setCode(-1); // 0：成功
				result.setMsg("用户编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			Integer beginRow = (page - 1) * pageSize + 1;
			pMap.put("beginRow", beginRow);
			pMap.put("endRow", pageSize * page);
			pMap.put("uid", uid);
			pMap.put("typeID", typeID);

			List<HashMap<String, Object>> data = this.mybatisService.findMybatisList("getMedicineListSQL", pMap);
			Long count = this.mybatisService.findIbatisListCount("getMedicineListCountSQL", pMap);
			count = count != null ? count : 0;
			result.setCode(0); // 0：成功
			result.setData(data);
			result.setRecordCount(count.intValue());
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

	// 患者-获取药品详情
	@RequestMapping("getMedicineDetail")
	public void getMedicineDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			// 药品编号
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";

			if ("".equals(id)) {
				result.setCode(-1); // 0：成功
				result.setMsg("药品编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("id", id);

			List<HashMap<String, Object>> iList = this.mybatisService.findMybatisList("getMedicineDetailSQL", pMap);
			List<HashMap<String, Object>> gList = this.mybatisService.findMybatisList("getMedicineAppDetailSQL", pMap);
			HashMap<String, Object> rMap = new HashMap<String, Object>();
			rMap.put("image", iList);
			rMap.put("gList", gList);
			result.setCode(0); // 0：成功
			result.setData(rMap);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}
	
	// 患者-获取药品图文详情
		@RequestMapping("getMedicineITDetail")
		public void getMedicineITDetail(HttpServletRequest request, HttpServletResponse response) throws Exception {
			// 返回结果
			HttpResult result = new HttpResult();
			try {
				ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
				if (paramBean == null) {
					result.setCode(1003); // 0：成功
					this.ajaxResultJson(response, result);
					return;
				}
				Map<String, Object> paramMap = paramBean.getParam();
				// 用户ID
				String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
				// 药品编号
				String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";

				if ("".equals(id)) {
					result.setCode(-1); // 0：成功
					result.setMsg("药品编号参数为空");
					this.ajaxResultJson(response, result);
					return;
				}

				HashMap<String, Object> pMap = new HashMap<String, Object>();
				pMap.put("id", id);

				List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getMedicineITDetailSQL", pMap);
				if(list!=null && list.size()>0){
					result.setData(list.get(0));
				} 
				result.setCode(0); // 0：成功
				this.ajaxResultJson(response, result);

			} catch (Exception e) {
				e.printStackTrace();
				result.setCode(-1); // 0：成功
				result.setMsg(e.getMessage());
				;// //错误消息(如无错误消息则留空)
				this.ajaxResultJson(response, result);
			}
		}

	// 患者-获取药品评价
	@RequestMapping("getMedicineAppraise")
	public void getMedicineAppraise(HttpServletRequest request, HttpServletResponse response) throws Exception {
		// 返回结果
		HttpResult result = new HttpResult();
		try {
			ParamBean paramBean = JSONUtils.fromJson(super.getJsonParam(request), ParamBean.class);
			if (paramBean == null) {
				result.setCode(1003); // 0：成功
				this.ajaxResultJson(response, result);
				return;
			}
			Map<String, Object> paramMap = paramBean.getParam();
			// 用户ID
			String uid = paramBean.getLimit().get("uid") != null ? paramBean.getLimit().get("uid").toString() : "";
			String id = paramMap.get("id") != null ? paramMap.get("id").toString() : "";

			if ("".equals(id)) {
				result.setCode(-1); // 0：成功
				result.setMsg("药品编号参数为空");
				this.ajaxResultJson(response, result);
				return;
			}

			HashMap<String, Object> pMap = new HashMap<String, Object>();
			pMap.put("id", id);
			int appCount, appGood, appCommon, appBad;
			int appResult;
			String jsondata = "";
			List<MedicineAppInfo> gList = new ArrayList<MedicineAppInfo>();
			List<MedicineAppInfo> mList = new ArrayList<MedicineAppInfo>();
			List<MedicineAppInfo> bList = new ArrayList<MedicineAppInfo>();
			try {
				List<HashMap<String, Object>> list = this.mybatisService.findMybatisList("getMedicineAppraiseSQL", pMap);
				jsondata = JSON.toJSON(list).toString();
			} catch (Exception e) {
				// TODO: handle exception
			}

			TypeToken<ArrayList<MedicineAppInfo>> token = new TypeToken<ArrayList<MedicineAppInfo>>() {
			};
			List<MedicineAppInfo> dList = JSONUtils.fromJson(jsondata, token);
			for (int i = 0; i < dList.size(); i++) {
				MedicineAppInfo dInfo = dList.get(i);
				if (dInfo != null) {
					appResult = dInfo.getAppResult();
					switch (appResult) {
					case 1:
						gList.add(dInfo);
						break;
					case 2:
						mList.add(dInfo);
						break;
					case 3:
						bList.add(dInfo);
						break;
					default:
						break;
					}
				}
			}

			HashMap<String, Object> rMap = new HashMap<String, Object>();
			rMap.put("appCount", dList.size());
			rMap.put("appGood", gList.size());
			rMap.put("appCommon", mList.size());
			rMap.put("appBad", bList.size());
			rMap.put("gList", gList);
			rMap.put("mList", mList);
			rMap.put("bList", bList);
			result.setCode(0); // 0：成功
			result.setData(rMap);
			this.ajaxResultJson(response, result);

		} catch (Exception e) {
			e.printStackTrace();
			result.setCode(-1); // 0：成功
			result.setMsg(e.getMessage());
			;// //错误消息(如无错误消息则留空)
			this.ajaxResultJson(response, result);
		}
	}

}
